import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;


public class User {
	private String ip;
	private String AWSAccessKeyId;
	private String AMIid;
	private String volumnId;
	private AWSCredentials credentials;
	
	public User(AWSCredentials credentials, String ip, String AMIid, String volumnId)
	{
		this.ip=ip;
		this.AWSAccessKeyId=credentials.getAWSAccessKeyId();
		this.AMIid=AMIid;
		this.volumnId=volumnId;
		this.credentials=credentials;
	}
	
	
	public User(AWSCredentials credentials, String ip, String volumnId)
	{
		this.ip=ip;
		this.AWSAccessKeyId=credentials.getAWSAccessKeyId();
		this.volumnId=volumnId;
		this.credentials=credentials;
	}
	
	public User(AWSCredentials credentials)
	{
		this.credentials=credentials;
	}
		
	public boolean isReturningUser()
	{
		return this.credentials.getAWSAccessKeyId()==this.AWSAccessKeyId;
	}
	
	public String getIp()
	{
		return ip;
	}
	
	public void setIp(String ip)
	{
		this.ip=ip;
	}
	
	public void setVolumnId(String volumnId)
	{
		this.volumnId=volumnId;
	}
	
	public void setAMIid(String AMIid)
	{
		this.AMIid=AMIid;
	}

	
	public String getAMIid()
	{
		return AMIid;
	}
	
	public String getVolumnId()
	{
		return volumnId;
	}
	
	public String getAWSAccessKeyId()
	{
		return AWSAccessKeyId;
	}
	
	public AWSCredentials getCredentials()
	{
		return this.credentials;
	}
	
	
	public static User getUser(AWSCredentials credentials)
	{
		DataBase db=new DataBase(credentials);
		ArrayList<HashMap<String,String>> arr=db.SelectQuery("select * from "+DataBase.table+" where AWSAccessKeyId='"+credentials.getAWSAccessKeyId()+"'");
		if(arr.size()>0)
		{
		User u=new User(credentials);
		u.ip=arr.get(0).get("ip");
		u.AWSAccessKeyId=arr.get(0).get("AWSAccessKeyId");
		u.AMIid=arr.get(0).get("AMIid");
		u.volumnId=arr.get(0).get("volumnId");
		u.credentials=credentials;
		return u;
		}
		else return null;
	}
	
	public void persist()
	{
		HashMap<String,String> hm =new HashMap<String,String>();
		hm.put("ip", this.ip);
		hm.put("AWSAccessKeyId", this.AWSAccessKeyId);
		hm.put("AMIid", this.AMIid);
		hm.put("volumnId", this.volumnId);
		DataBase db=new DataBase(this.credentials);
		db.InsertQuery(hm);
		
	}
	
	public void update(String attr, String value)
	{
		DataBase db=new DataBase(this.credentials);
		db.UpdateQuery(attr,value,credentials.getAWSAccessKeyId());
	}
	
	public String toString()
	{
		return this.ip+" "+this.AMIid+" "+this.AWSAccessKeyId+" "+this.volumnId;
	}
	
	public static void main(String... args)
	{
		AWSCredentials credentials=null;
		try {
			credentials = new PropertiesCredentials(User.class.getResourceAsStream("AwsCredentials.properties"));
			System.out.println("1"+credentials);
		} catch (IOException e) {
			e.printStackTrace();
		}
		DataBase db=new DataBase(credentials);
		if(!db.Exist(DataBase.table))
			db.createTable(DataBase.table);
		System.out.println(db.Exist(DataBase.table));
		User u2=getUser(credentials);
		System.out.println(u2);
	
		User u=new User(credentials,"iptestttt","amitestttttt","kkk");
		db.SelectQuery("select * from "+DataBase.table);
	}
}
